package br.com.crednove.paineis;

import br.com.crednove.dao.DAOCliente;
import br.com.crednove.modelo.Cliente;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;

public class PainelGraficosPizza extends javax.swing.JFrame {

    private DAOCliente daoCliente;
    private ArrayList<Cliente> listaClientes;
    private String listaCidadesRobson[] = {"AMERICANA", "ARTUR NOGUEIRA", "BOA VISTA", "CAMPINAS", "HOLAMBRA", 
        "HORTOLANDIA", "HORTOLÂNDIA", "INDAIATUBA", "JAGUARIUNA", "LOUVEIRA", "MONTE MOR", "NOVA ODÉSSA", "NOVA ODESSA", 
        "PAULÍNEA", "PAULINEA", "PEDREIRA", "SALTO", "SANTA BARBARA DOESTE", "SANTA BARBARA D OESTE", "SUMARÉ", "SUMARE", 
        "VALINHOS", "VINHEDO", "ITÚ", "ITU", "AMPARO", "LIMEIRA", "MOGI MIRIM", "SERRA NEGRA", "ARARAS", "MOGI GUAÇU", 
        "MOGI GUACU", "CAPIVARI", "IRACEMAPOLIS", "LEME", "RIO CLARO", "PIRACICABA", "ITAPIRA", "BRAGANÇA PAULISTA", 
        "BRAGANCA PAULISTA", "ATIBAIA", "AGUAS DE LINDOIA", "SOROCABA", "TIETÊ", "TIETE"};
    
    private String listaCidadesVanderlei[] = {"ARUJA", "BARUERI", "CAIEIRAS", "CAMPO LIMPO PTA", 
        "CAMPO LIMPO PAULISTA", "CARAPICUÍBA", "CAUCAIA DO ALTO", "COTIA", "DIADEMA", "EMBU", "EMBU DAS ARTES", 
        "EMBU GUAÇU", "EMBU GUACU", "FERRAZ DE VASCONCELOS" , "FRANCISCO MORATO", "FRANCO DA ROCHA", "GUARULHOS", 
        "ITAPECERICA DA SERRA", "ITAPEVI", "ITAQUAQUECETUBA", "JANDIRA", "JUNDIAÍ", "JUNDIAI", 
        "MAUÁ", "MAUÁ", "MOGI DAS CRUZES", "MOJI DAS CRUZES", "OSASCO", "PARELHEIROS", "POÁ", "POA", 
        "RIBEIRÃO PIRES", "RIBEIRAO PIRES", "RIO GRANDE DA SERRA", "SANTA BARBARA DOESTE","SANTANA DE PARNAÍBA", 
        "SANTANA DE PARNAIBA", "SANTO AMARO", "SANTO ANDRÉ", "SANTO ANDRE", "STO ANDRE", "SÃO BERNARDO DO CAMPO", 
        "SAO BERNARDO DO CAMPO", "S BERNARNDO DO CAMPO", "S B C", "S B CAMPO", "SÃO CAETANO DO SUL", "SAO CAETANO DO SUL", 
        "SAO CAETANO", "SÃO PAULO", "SAO PAULO", "S PAULO", "SP", "SÃO BERNARDO DO CAMPO", 
        "SAO BERNARDO DO CAMPO", "SUZANO", "TABOÃO DA SERRA", "TABOAO DA SERRA", 
        "VARGEM GRANDE", "VARZEA PTA"};

    public PainelGraficosPizza() {
        this.daoCliente = new DAOCliente();
        this.listaClientes = new ArrayList<>();
        initComponents();
        setLocationRelativeTo(null);

        JFreeChart graf = ChartFactory.createPieChart3D("Trabalhados x Não Trabalhados", createDataset1(), true, false, true);
        ChartPanel myChartPanel = new ChartPanel(graf, true);
        myChartPanel.setSize(jPanel1.getWidth(), jPanel1.getHeight());
        myChartPanel.setVisible(true);
        jPanel1.removeAll();
        jPanel1.add(myChartPanel);
        jPanel1.revalidate();
        jPanel1.repaint();
        
        graf = ChartFactory.createPieChart3D("Robson x Vanderlei x Rota Log", createDataset2(), true, false, true);
        myChartPanel = new ChartPanel(graf, true);
        myChartPanel.setSize(jPanel2.getWidth(), jPanel2.getHeight());
        myChartPanel.setVisible(true);
        jPanel2.removeAll();
        jPanel2.add(myChartPanel);
        jPanel2.revalidate();
        jPanel2.repaint();
    }

    private DefaultPieDataset createDataset1() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        this.daoCliente.conectar();
        this.daoCliente.executaSQL("SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_EXPORTADO = FALSE");
        int numNaoExportados = this.daoCliente.getNumRegistros();
        this.daoCliente.executaSQL("SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_EXPORTADO = TRUE AND CLIE_TRABALHADO = FALSE");
        int numNaoTrabalhados = this.daoCliente.getNumRegistros();
        this.daoCliente.executaSQL("SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_TRABALHADO = TRUE");
        int numTrabalhados = this.daoCliente.getNumRegistros();
        this.daoCliente.desconectar();

        dataset.setValue("Não Exportados\n" + numNaoExportados, numNaoExportados);
        dataset.setValue("Não Trabalhados\n" + numNaoTrabalhados, numNaoTrabalhados);
        dataset.setValue("Trabalhados\n" + numTrabalhados, numTrabalhados);

        return dataset;
    }

    private DefaultPieDataset createDataset2() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        this.daoCliente.conectar();

        //BOY 1
        String sql = "SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_ESTADO='SP' AND (";
        for (int i = 0; i < this.listaCidadesVanderlei.length; i++) {
            sql = sql + "clie_cidade='" + this.listaCidadesVanderlei[i] + "'";
            if (i != this.listaCidadesVanderlei.length - 1) {
                sql = sql + " OR ";
            }
        }
        sql = sql + ");";
        this.daoCliente.executaSQL(sql);
        int numBoy1 = this.daoCliente.getNumRegistros();
        
        //BOY 2
        sql = "SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_ESTADO='SP' AND (";
        for (int i = 0; i < this.listaCidadesRobson.length; i++) {
            sql = sql + "clie_cidade='" + this.listaCidadesRobson[i] + "'";
            if (i != this.listaCidadesRobson.length - 1) {
                sql = sql + " OR ";
            }
        }
        sql = sql + ");";
        this.daoCliente.executaSQL(sql);
        int numBoy2 = this.daoCliente.getNumRegistros();

        //ROTALOG
        sql = "SELECT CLIE_TRABALHADO FROM CLIENTE";
        this.daoCliente.executaSQL(sql);
        int numRotaLog = this.daoCliente.getNumRegistros() - (numBoy1 + numBoy2);
        
        dataset.setValue("ROTA LOG\n"+numRotaLog, numRotaLog);
        dataset.setValue("Vanderlei\n"+numBoy1, numBoy1);
        dataset.setValue("Robson\n"+numBoy2, numBoy2);

        this.daoCliente.desconectar();
        return dataset;
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gráficos de Pizza");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Gráfico 1"));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 340, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Gráfico 2"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 340, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
